﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

 namespace FreeSqlMySqlExtensions
{
   
    public static class IdExtension
    {
       

        public static DateTime GetDateTimeFromId(this long id)
        {
            var Id2Str = Convert.ToString(id, 2);
            var TimeStr = Id2Str.Substring(0, Id2Str.Length - IdHelper.IdGeneratorOptions.WorkerIdBitLength - IdHelper.IdGeneratorOptions.SeqBitLength);
            var dt = IdHelper.IdGeneratorOptions.BaseTime.AddMilliseconds(Convert.ToInt64(TimeStr, 2)).ToLocalTime();
            return dt;
        }
    }
}
public class IdHelper : YitIdHelper
{

    public static bool HasSetId { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public static IdGeneratorOptions IdGeneratorOptions { get; set; } = new IdGeneratorOptions();

    public static void SetIdGeneratorOptions(ushort workerId = 1, byte workerIdBitLength = 10)
    {
        if (!IdHelper.HasSetId)
        {
            var options = new IdGeneratorOptions(workerId);
            options.WorkerIdBitLength = workerIdBitLength;
            IdHelper.SetIdGenerator(options);
            IdHelper.IdGeneratorOptions = options;
            IdHelper.HasSetId = true;
        }
    }
}